package wtws.manager.in;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import wtws.manager.in.interf.ProductDecoder;
import wtws.message.AlphanumericProduct;
import wtws.message.GraphicMicrobustProduct;
import wtws.message.detail.AlphanumericProductSubBlock;
import wtws.message.detail.GraphicMicroBustProductSubBlock;
import wtws.message.interf.TdwrProdMessage;

public class AlphaMbDecoder implements ProductDecoder {

	private static final int NUMBER_OF_RUNWAY_WARNING = 8;
	private static final int ALPHA_LINE_LENGTH = 17;
	private static final int KM2M = 1000;
	private static final String MBA_TEXT = "MBA";

	public static ArrayList<TdwrProdMessage> processFile(File file) throws FileNotFoundException, ParseException {
		ArrayList<TdwrProdMessage> qList = new ArrayList<TdwrProdMessage>();
		int lnNumOfLine = 0;
		long llTimestamp = 0L;

		ArrayList<AlphanumericProductSubBlock> alphaSubs = new ArrayList<AlphanumericProductSubBlock>();
		ArrayList<GraphicMicroBustProductSubBlock> mbSubs = new ArrayList<GraphicMicroBustProductSubBlock>();

		Scanner fin = new Scanner(file);
		int alpanAlertCount = 0;
		while (fin.hasNextLine()) {
			String line = fin.nextLine().trim();
			lnNumOfLine++;
			if (lnNumOfLine == 1) {
				Date date = new SimpleDateFormat("yyyyMMdd'T'HHmmss").parse(line);
				llTimestamp = date.getTime() / 1000 + 8 * 60 * 60;
			} else if (lnNumOfLine <= NUMBER_OF_RUNWAY_WARNING + 1) {
				if (line.length() >= ALPHA_LINE_LENGTH) {
					Integer arenaLoc = AlphanumericProductSubBlock.ARENA_LOCATION.get(line.substring(line.length() - 3));
					if (arenaLoc == null)
						arenaLoc = 0;
					alphaSubs.add(new AlphanumericProductSubBlock(arenaLoc, 1, alphaSubs.size() + 1, line.charAt(3), line));
					alpanAlertCount++;
				} else {
					alphaSubs.add(new AlphanumericProductSubBlock(0, 0, alphaSubs.size() + 1, line.charAt(3), line));
				}
			} else {
				String[] mbValues = line.split(",");
				if (mbValues.length == 7) {
					int X1, Y1, X2, Y2, Radius, Loss;
					X1 = (int) (KM2M * (Double.parseDouble(mbValues[0])));
					Y1 = (int) (KM2M * (Double.parseDouble(mbValues[1])));
					X2 = (int) (KM2M * (Double.parseDouble(mbValues[2])));
					Y2 = (int) (KM2M * (Double.parseDouble(mbValues[3])));
					Radius = (int) (KM2M * (Double.parseDouble(mbValues[4])));
					Loss = Integer.parseInt(mbValues[5]);
					short solidShape = (short) (MBA_TEXT.equals(mbValues[6]) ? 1 : 0);
					System.out.println("[ALERT] MB " + Loss);
					mbSubs.add(new GraphicMicroBustProductSubBlock(X1, Y1, X2, Y2, Radius, Loss, solidShape));
				}
			}
		}
		fin.close();
		if (alpanAlertCount > 0) {
			AlphanumericProduct lrAlphaProduct = new AlphanumericProduct(0, alphaSubs);
			qList.add(lrAlphaProduct);
		}
		if (mbSubs.size() != 0) {
			GraphicMicrobustProduct lrGmd = new GraphicMicrobustProduct(llTimestamp, mbSubs);
			qList.add(lrGmd);
		}
		return qList;

	}

}
